<!-- src/views/login/index.vue -->
<template>
  <div class="loginContianer">
    <div class="loginBox">
      <h1 class="loginTitle">嗨购管理系统</h1>
      <el-form ref="ruleFormRef" :model="ruleForm" status-icon :rules="rules" class="demo-ruleForm">
        <el-form-item prop="adminname">
          <el-input
            :prefix-icon="User"
            v-model="ruleForm.adminname"
            placeholder="管理员账户"
            type="text"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            :prefix-icon="Lock"
            show-password
            v-model="ruleForm.password"
            placeholder="管理员密码"
            type="password"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item>
          <el-button style="width: 100%" type="primary" @click="submitForm(ruleFormRef)"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
      <div class="loginTip">账号： admin 密码：123456</div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import { login } from '@/api/admin'
import { useRouter } from 'vue-router'
import { useAdminStore } from '@/stores/admin'

const ruleFormRef = ref<FormInstance>()

const ruleForm = reactive({
  adminname: '',
  password: ''
})

const validateAdminname = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入管理员账户'))
  } else {
    callback()
  }
}

const validatePass = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入密码'))
  } else {
    callback()
  }
}

const rules = reactive<FormRules>({
  adminname: [{ validator: validateAdminname, trigger: 'blur' }],
  password: [{ validator: validatePass, trigger: 'blur' }]
})
const router = useRouter()
const store = useAdminStore()
const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      console.log('submit!', ruleForm)
      login(ruleForm).then((res) => {
        console.log('res', res)
        if (res.data.code === '10003') {
          ElMessage.warning('密码错误')
        } else if (res.data.code === '10005') {
          ElMessage.error('该账户还未注册')
        } else {
          ElMessage.success('登录成功')

          const result = res.data.data
          store.changeAdminname(result.adminname)
          store.changeCheckedkeys(result.checkedkeys)
          store.changeToken(result.token)
          store.changeRole(result.role)
          router.replace('/')
        }
      })
    } else {
      console.log('error submit!')
      return false
    }
  })
}
</script>

<style>
.loginContianer {
  height: 100%;
  width: 100%;
  background-color: #2d3a4b;
  display: flex;
  justify-content: center;
  align-items: center;
}

.loginBox {
  width: 520px;
  padding: 35px;
  box-sizing: border-box;
}
.loginTitle {
  text-align: center;
  margin: 50px 0;
  color: #fff;
}
.el-input {
  background-color: #454545;
  background: rgba(0, 0, 0, 0.1);
  height: 50px;
}
.el-form-item {
  border: 1px solid hsla(0, 0%, 100%, 0.1);
  border-radius: 5px;
}
.el-input__wrapper {
  background-color: transparent;
}
.el-input__inner {
  color: #fff;
}
.loginTip {
  color: #fff;
}
</style>
